import pyautogui as pyg
import pygetwindow as gw
import keyboard
import time
import os
import pyscreeze
from Tools import Tools


# 打包方法 pyinstaller.exe --onefile --add-data "image:image"  龙服-辅助.py

class Zengfu:
    pause = True
    SUCCESS_IMAGE = os.path.join(os.path.join(os.path.dirname(__file__), "image"), "duanzao_success.png")
    SUCCESS_IMAGE_NOT_ME = os.path.join(os.path.join(os.path.dirname(__file__), "image"), "not_me_success.png")
    FAIL_COMMIT_IMAGE = os.path.join(os.path.join(os.path.dirname(__file__), "image"), "baohu_commit.png")
    DNF_WD = gw.getWindowsWithTitle("Dungeon & Fighter")[0]
    INIT_WIDTH = 800
    INIT_HEIGHT = 600
    tools = Tools(DNF_WD, init_width=INIT_WIDTH, init_height=INIT_HEIGHT)

    def __init__(self, baohu_size, success_limit=3, has_success_limit=0):
        self.BH_LIMIT = baohu_size
        self.SUCCESS_LIMIT = success_limit
        self.HAS_SUCCESS_LIMIT = has_success_limit

    def click_dnf(self,x, y):
        pyg.moveTo(x, y, duration=0.1)
        pyg.mouseDown(button="left")
        pyg.mouseUp(button="left")

    def pause_zengfu(self):
        self.pause = True
        print("暂停增福")

    def start_zengfu(self):
        self.pause = False
        print("开始增福")

    def zengfu(self):
        # 增幅按钮位置
        if self.HAS_SUCCESS_LIMIT >= self.SUCCESS_LIMIT:
            print(f"已经成功增幅次数：{self.HAS_SUCCESS_LIMIT},最大次数限制：{self.SUCCESS_LIMIT},增幅次数：{self.zengfu_count}")
            print(f"请按ctrl+F12重置增幅成功次数")
            self.pause = True
            return
        (x, y) = tools.compute_position(-110, 55)
        self.click_dnf(x, y)
        # 移动到装备第一格
        (x, y) = tools.compute_position(85, -10)
        self.click_dnf(x, y)
        # 确认增幅 X=371, Y=383
        (x, y) = tools.compute_position(-30, 85)
        self.click_dnf(x, y)
        # 保护卷确认
        (x, y) = tools.compute_position(-30, 50)
        self.click_dnf(x, y)
        time.sleep(2.0)
        is_success = tools.find_img_abs_path(self.SUCCESS_IMAGE, count=5, region=(
        self.DNF_WD.left, self.DNF_WD.top, self.DNF_WD.left + self.DNF_WD.width, self.DNF_WD.top + self.DNF_WD.height))
        if is_success != True:
            is_success = self.find_img_click(self.SUCCESS_IMAGE_NOT_ME, count=5, region=(
            self.DNF_WD.left, self.DNF_WD.top, self.DNF_WD.left + self.DNF_WD.width, self.DNF_WD.top + self.DNF_WD.height))
        if is_success == True:
            print(f"增幅成功！,成功图片发现结果：{is_success}")
            self.HAS_SUCCESS_LIMIT = self.HAS_SUCCESS_LIMIT + 1

        # 失败确认位置 410 350
        (x, y) = tools.compute_position(0, 30)
        self.click_dnf(x, y)
        # 失败确认位置2 406, Y=352
        (x, y) = tools.compute_position(5, 40)
        self.click_dnf(x, y)
        time.sleep(0.5)
